1 using System;
2 using
UnityEngine;
3 using
UnityStandardAssets.CrossPlatformInput;
4
5 namespace
UnityStandardAssets.Cameras
6 {
7     
public class FreeLookCam : PivotBasedCameraRig
8     {
9         
// This script is designed to be placed on the root object of a camera rig,
10         
// comprising 3 gameobjects, each parented to the next:
11
12         
// Camera Rig
13         
// Pivot
14         
// Camera
15
16         [SerializeField]
private float m_MoveSpeed = 1f; // How fast the rig will move to keep up with the target's position.
17         [Range(
0f, 10f)] [SerializeField] private float m_TurnSpeed = 1.5f; // How fast the rig will rotate from user input.
18         [SerializeField]
private float m_TurnSmoothing = 0.1f; // How much smoothing to apply to the turn input, to reduce mouse-turn jerkiness
19         [SerializeField]
private float m_TiltMax = 75f; // The maximum value of the x axis rotation of the pivot.
20         [SerializeField]
private float m_TiltMin = 45f; // The minimum value of the x axis rotation of the pivot.
21         [SerializeField]
private bool m_LockCursor = false; // Whether the cursor should be hidden and locked.
22         [SerializeField]
private bool m_VerticalAutoReturn = false; // set wether or not the vertical axis should auto return
23
24         
private float m_LookAngle; // The rig's y axis rotation.
25         
private float m_TiltAngle; // The pivot's x axis rotation.
26         
private const float k_LookDistance = 100f; // How far in front of the pivot the character's look target is.
27         
private Vector3 m_PivotEulers;
28         
private Quaternion m_PivotTargetRot;
29         
private Quaternion m_TransformTargetRot;
30
31         
protected override void Awake()
32         {
33             
base.Awake();
34             
// Lock or unlock the cursor.
35             Cursor.lockState = m_LockCursor ? CursorLockMode.Locked : CursorLockMode.None;
36             Cursor.visible = !m_LockCursor;
37             m_PivotEulers = m_Pivot.rotation.eulerAngles;
38
39             m_PivotTargetRot = m_Pivot.transform.localRotation;
40             m_TransformTargetRot = transform.localRotation;
41         }
42
43
44         
protected void Update()
45         {
46             HandleRotationMovement();
47             
if (m_LockCursor && Input.GetMouseButtonUp(0))
48             {
49                 Cursor.lockState = m_LockCursor ? CursorLockMode.Locked : CursorLockMode.None;
50                 Cursor.visible = !m_LockCursor;
51             }
52         }
53
54
55         
private void OnDisable()
56         {
57             Cursor.lockState = CursorLockMode.None;
58             Cursor.visible =
true;
59         }
60
61
62         
protected override void FollowTarget(float deltaTime)
63         {
64             
if (m_Target == null) return;
65             
// Move the rig towards target position.
66             transform.position = Vector3.Lerp(transform.position, m_Target.position, deltaTime*m_MoveSpeed);
67         }
68
69
70         
private void HandleRotationMovement()
71         {
72             
if(Time.timeScale < float.Epsilon)
73             
return;
74
75             
// Read the user input
76             
var x = CrossPlatformInputManager.GetAxis("Mouse X");
77             
var y = CrossPlatformInputManager.GetAxis("Mouse Y");
78
79             
// Adjust the look angle by an amount proportional to the turn speed and horizontal input.
80             m_LookAngle += x*m_TurnSpeed;
81
82             
// Rotate the rig (the root object) around Y axis only:
83             m_TransformTargetRot = Quaternion.Euler(
0f, m_LookAngle, 0f);
84
85             
if (m_VerticalAutoReturn)
86             {
87                 
// For tilt input, we need to behave differently depending on whether we're using mouse or touch input:
88                 
// on mobile, vertical input is directly mapped to tilt value, so it springs back automatically when the look input is released
89                 
// we have to test whether above or below zero because we want to auto-return to zero even if min and max are not symmetrical.
90                 m_TiltAngle = y >
0 ? Mathf.Lerp(0, -m_TiltMin, y) : Mathf.Lerp(0, m_TiltMax, -y);
91             }
92             
else
93             {
94                 
// on platforms with a mouse, we adjust the current angle based on Y mouse input and turn speed
95                 m_TiltAngle -= y*m_TurnSpeed;
96                 
// and make sure the new value is within the tilt range
97                 m_TiltAngle = Mathf.Clamp(m_TiltAngle, -m_TiltMin, m_TiltMax);
98             }
99
100             
// Tilt input around X is applied to the pivot (the child of this object)
101             m_PivotTargetRot = Quaternion.Euler(m_TiltAngle, m_PivotEulers.y , m_PivotEulers.z);
102
103             
if (m_TurnSmoothing > 0)
104             {
105                 m_Pivot.localRotation = Quaternion.Slerp(m_Pivot.localRotation, m_PivotTargetRot, m_TurnSmoothing * Time.deltaTime);
106                 transform.localRotation = Quaternion.Slerp(transform.localRotation, m_TransformTargetRot, m_TurnSmoothing * Time.deltaTime);
107             }
108             
else
109             {
110                 m_Pivot.localRotation = m_PivotTargetRot;
111                 transform.localRotation = m_TransformTargetRot;
112             }
113         }
114     }
115 }


Gõ tìm kiếm nhanh...